<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTrainingSessionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('training_sessions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('character_id')->nullable();
            $table->unsignedBigInteger('scenario_id')->nullable();
            $table->string('session_uuid', 36)->unique();
            $table->enum('status', ['active', 'completed', 'failed', 'abandoned'])->default('active');
            $table->unsignedTinyInteger('initial_difficulty')->default(1);
            $table->unsignedTinyInteger('final_difficulty')->nullable();
            $table->unsignedTinyInteger('final_emotion_score')->nullable()->comment('最终AI情绪分数');
            $table->boolean('success_achieved')->default(false);
            $table->timestamp('started_at')->useCurrent();
            $table->timestamp('completed_at')->nullable();
            $table->text('session_summary')->nullable()->comment('会话总结数据(JSON字符串)');
            
            // 外键约束
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('character_id')->references('id')->on('ai_characters');
            $table->foreign('scenario_id')->references('id')->on('training_scenarios');
            
            // 索引
            $table->index(['user_id']);
            $table->index(['status']);
            $table->index(['started_at']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('training_sessions');
    }
}